package com.wbc.smartHome.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.wbc.smartHome.dao.CommodityTypeDao;
import com.wbc.smartHome.model.CommodityType;
import com.wbc.smartHome.model.Result;
import com.wbc.smartHome.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CommodityTypeService {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CommodityTypeDao commodityTypeDao;

    private String key = "commoditytype";

    public void setCommodityType(CommodityType commodityType, String adminToken) {
      /*ListOperations listOps  = redisTemplate.opsForList();
        listOps.rightPush(key, CommodityType);*/
        int id = JWTUtil.getAdmin(adminToken).getId();
        commodityTypeDao.setCommodityType(commodityType.getName(),commodityType.getContent(),commodityType.getImg(),id);
    }

    public PageInfo<CommodityType> getCommodityTypeList(CommodityType commodityType) {
        /*ListOperations listOps  = redisTemplate.opsForList();
        List<CommodityType> commodityTypeList = listOps.range(key, 0, -1);*/
        PageHelper.startPage(commodityType.getPageNo(),commodityType.getPageSize());
        List<CommodityType> commodityTypeList = commodityTypeDao.getCommodityTypeList(commodityType);
        PageInfo<CommodityType> pageInfo = new PageInfo<>(commodityTypeList);
        return pageInfo;
    }

    public void delete(int id) {
        commodityTypeDao.delete(id);
    }

    public void updateCommodityType(CommodityType commodityType, String adminToken) {
        int id = JWTUtil.getAdmin(adminToken).getId();
        commodityTypeDao.updateCommodityType(commodityType.getId(),commodityType.getName(),commodityType.getContent(),commodityType.getImg(),id);
    }

    public Result uploadImg(CommonsMultipartFile commonsMultipartFile) throws IOException {
        //为了避免上传的文件重名,重新生成一个文件名
        String newFileName = new Date().getTime()+commonsMultipartFile.getOriginalFilename();
        //连接oss
        OSS ossClient = new OSSClientBuilder().build("https://"+endpoint, accessKeyId, accessKeySecret);
        //长传文件
        ossClient.putObject(bucketName, newFileName, commonsMultipartFile.getInputStream());
        //拼接文件在oss中访问地址
        String url = "https://"+bucketName+"."+endpoint+"/"+newFileName;
        //newsDao.savePhoto(url);
        return new Result(200,"操作成功",url);
    }

    public CommodityType getCommodityTypeById(int id) {
        CommodityType commodityType = commodityTypeDao.getCommodityTypeById(id);
        return commodityType;
    }
}
